Ext.onReady(function() {
	//显示提示信息
	Ext.QuickTips.init();
	
	//统一设置提示风格，为side
	Ext.form.Field.prototype.msgTarget='side';	
	
	//片路径：一张1×1的透明图片，Ext使用它来和CSS一起创建内嵌的图标。 
	Ext.BLANK_IMAGE_URL = '../extjs/resources/images/default/s.gif';
	
	//checkbox选择模型
	var selModel = new Ext.grid.CheckboxSelectionModel();
	
	//数据源
 	var userStore = new Ext.data.JsonStore({
			url: '../employee/list.action',
			fields: ['empId', 'empLoginName', 'empName','depId','depName','levId','levName'],
			root: 'data', 
			totalProperty: 'total' 
		});
 	userStore.load({
		params: {
			start: 0,
			limit: 10
		}
	});	
 	
 	//checkbox选择模型
	var selModel = new Ext.grid.CheckboxSelectionModel();
	
	//列模型
	var colModel = new Ext.grid.ColumnModel([
		new Ext.grid.RowNumberer({header: '序号', width: 35}), //加入序号
		selModel, //加入checkbox选择模型
		{header: '登录名', width: 100, dataIndex: 'empLoginName', sortable: true},
		{header: '真实姓名', width: 100, dataIndex: 'empName', sortable: true},
		{header: '所属部门', width: 100, dataIndex: 'depName', sortable: true},
		{header: '所属角色', width: 100, dataIndex: 'levName', sortable: true}
	]);
	colModel.defaultWidth  = 100;
	
	var userGrid = new Ext.grid.GridPanel({
		title: '职员列表',
		height: 450,
		autoWidth: true, 
		viewConfig: new Ext.grid.GridView({
			autoFill: true  
		}),
		stripeRows: true, 
		sm: selModel, 
		frame: true, 
		applyTo: 'employee_div',
		store: userStore,
		cm: colModel, //列模型
		tbar: new Ext.Toolbar([
			{text: '添加', icon: '../images/add.gif', handler: showAdd},
			{text: '修改', icon: '../images/edit.gif', handler: showUpdate},
			{text: '删除', icon: '../images/delete.gif', handler: doDel}
		]),
		bbar: new Ext.PagingToolbar({
			store: userStore, 
			pageSize: 10,
			displayInfo: true,
			plugins: new Ext.ux.ProgressBarPager()
		})
	});
	
	var userForm = new Ext.form.FormPanel({
		frame: true, 
		buttonAlign: 'center',
		defaultType: 'textfield',
		fileUpload: true, 
		defaults: {width: 200},
		items: [
			{
				id: 'empId',
				name: 'emp.empId',
				xtype: 'hidden'
			},
			{
				id: 'empName',
				name: 'emp.empName',
				fieldLabel: '真实姓名',
				allowBlank: false
			},
			{
				id: 'empLoginName',
				name: 'emp.empLoginName',
				fieldLabel: '登录名',
				allowBlank: false
			},
			{
				id: 'empPassword',
				name: 'emp.empPassword',
				inputType: 'password',
				fieldLabel: '登录密码',
				allowBlank: false
			},
			{
				id: 'depId',
				xtype: 'combo',
				hiddenName: 'emp.department.depId',
				mode: 'remote', 
				fieldLabel: '所属部门',
				store: new Ext.data.JsonStore({ 
					//data: [{id:1, name:'交换分局'}, {id:2, name:'数据分局'}],
					url: '../department/findList.action',
					fields: ['depId', 'depName'],
					autoLoad: true //自动调用load方法
				}),
				displayField: 'depName',
				valueField: 'depId',
				triggerAction: 'all',
				typeAhead: true,
				forceSelection: true, 
				emptyText: '请选择...'
			},
			{
				id: 'levId',
				xtype: 'combo',
				hiddenName: 'emp.empLevel.levId',
				mode: 'remote', 
				fieldLabel: '所属角色',
				store: new Ext.data.JsonStore({ 
					//data: [{id:1, name:'交换分局'}, {id:2, name:'数据分局'}],
					url: '../empLevel/findListby.action',
					fields: ['levId', 'levName'],
					autoLoad: true //自动调用load方法
				}),
				displayField: 'levName',
				valueField: 'levId',
				triggerAction: 'all',
				typeAhead: true,
				forceSelection: true, 
				emptyText: '请选择...'
			}
		],
		buttons: [
			{text: '保存', icon: '../images/save.gif', handler: doSave},
			{text: '关闭', icon: '../images/close.gif', handler: doClose}
		]	
	});		
	
	var win = new Ext.Window({
		width: 400,
		height: 260,
		items: [userForm],
		modal: true, //模态
		resizable: false, //禁制调整窗口大小
		closeAction: 'hide' //不销毁form对象，只是隐藏
	});
	
	function showAdd() {
		win.setTitle('添加职员');
		win.show();
		//修改为非只读
//		Ext.getDom('code').readOnly = false;
		//定位焦点
		//Ext.getDom('code').focus();
		Ext.getCmp('empName').focus(true, 500);
		userForm.getForm().reset(); //重置
	}
	
	function doSave() {
//		
//		alert(Ext.getDom('empName').value);
//		return;
		userForm.getForm().submit({
			url: '../employee/addOrUpdate.action',
			//url: 'user/add.action' //也是正确的，因为本来就在user目录下
			method: 'POST',
			clientValidation: true,
			waitTitle: '提示',
			waitMsg: '正在保存数据，请稍后...',
			success: function() {
				userStore.reload(); //重新加载数据
				Ext.Msg.alert('提示', '保存职员成功！', function() {
					win.hide();					
				});
			},
			failure: function() {
				Ext.Msg.alert('提示', '保存职员失败！');
			}
		});
	}
	
	function doClose() {
		win.hide();
	}
	
	function showUpdate() {
		var records = userGrid.getSelectionModel().getSelections();
		if (records.length == 0) {
			Ext.Msg.alert('提示', '请选择需要修改的数据！');	
		}else if (records.length > 1) {
			Ext.Msg.alert('提示', '一次只能修改一个数据！');
		}else {
			win.setTitle('修改职员信息');
			win.show();
			userForm.getForm().load({
				url: '../employee/edit.action',
				method: 'GET',
				params: {'emp.empId': records[0].get('empId')},
				waitTitle: '提示',
				waitMsg: '正在装载数据，请稍后...',
				failure: function() {
					Ext.Msg.alert('提示', '装载数据失败！');
				}
			});
		}
	}
	function doDel() {
		var records = userGrid.getSelectionModel().getSelections();
		if (records.length == 0) {
			Ext.Msg.alert('提示', '请选择需要删除的数据！');
		}else {
			Ext.Msg.confirm('提示', '确认删除？', function(data) {
				if (data == 'yes') {
					var empIds = [];	
					for (var i=0; i<records.length; i++) {
						empIds.push(records[i].get('empId'));
					}
					Ext.Msg.show({
						title: '提示',
						msg: '正在删除数据，请稍后...'
					});
					Ext.Ajax.request({
						url: '../employee/del.action',
						method: 'GET',
						params: {'empIds':empIds},
						callback: function(options, success, response ) {
							if (success) {
								userStore.reload(); //重新装载
								Ext.Msg.alert('提示', '职员删除成功！');
							}else {
								Ext.Msg.alert('提示', '职员删除失败！');
							}
						}
					});
				}
			});
		}
	}
	
});